m.interno = paciente.interno

Create cursor cConta ;
  ( ;
    Quem       C( 1), ;
    Medico     N( 2), ;
    Nome       C(40), ;
    ValReceber N(13,2) ;
  )
  
Select quem, medico, nome, ;
       sum( round( quantidade * (valor+valorfilme), 2) ) as ValReceber ;
  from Movmento a, Medicos b ;
 where a.Quem     = 'M'        ;
   and a.Medico   = b.Codigo   ;
   and a.Situacao = 'A'        ;
   and a.Paciente = m.Interno  ;
   and a.Presente              ;
 group by a.medico ;
 order by a.medico ;
  into cursor cTotMed
  
scan all
     scatter memvar
     insert into cConta from memvar
endscan
use

Select quem, medico, nome, ;
       sum( round( quantidade * (valor+valorfilme), 2) ) as ValReceber ;
  from Movmento a, Anestesi b ;
 where a.Quem     = 'A'        ;
   and a.Medico   = b.Codigo   ;
   and a.Situacao = 'A'        ;
   and a.Paciente = m.Interno  ;
   and a.Presente              ;
 group by a.medico ;
 order by a.medico ;
  into cursor cTotMed
  
scan all
     scatter memvar
     insert into cConta from memvar
endscan
use

Select quem, 0 as medico, 'Clinica' as nome,  ;
       sum( round( quantidade * (valor+valorfilme), 2) ) as ValReceber ;
  from Movmento a ;
 where a.Quem  not in ('M', 'A' )  ;
   and a.Situacao = 'A'        ;
   and a.Paciente = m.Interno  ;
   and a.Presente              ;
 order by a.medico ;
  into cursor cTotClin
  
scan all
     scatter memvar
     insert into cConta from memvar
endscan
use

Select cConta

Select Quem, Medico, Nome, sum( ValReceber ) as ValReceber ;
  from cConta ;
 group by quem, medico ;
 order by medico ;
  into cursor cConta 
  
